$(function(){
	
	$("#outRegister").click(function(){
		if (confirm("您真的要退出注册吗?")) {
			window.location.href = "/onlinehotel/index.jsp";
		}
	});
	
	$("#account").blur(function() {
		var account = $("#account").val();
		checkAccount(account)
	});
	
	$("#customerPwd").blur(function(){ 
		var customerPwd = $("#customerPwd").val() ;
		checkCustomerPwd(customerPwd)});
	
	$("#customerPwd2").blur(function(){ 
		var customerPwd = $("#customerPwd").val() ;
		var customerPwd2 = $("#customerPwd2").val() ;
		checkCustomerPwd2(customerPwd,customerPwd2)});
	
	$("#mailAccount").blur(function(){
		var mailAccount = $("#mailAccount").val() ;
		checkMailAccount(mailAccount)});
	
	$("#contactInfo").blur(function(){
		var contactInfo = $("#contactInfo").val();
		checkContactInfo(contactInfo)});

	$("#account").focus(function() {
		accountSpan.innerHTML = "<font size=2 color=red>·为了方便记忆,您可用手机号注册!</font>";
	});
	
	$("#mailAccount").focus(function() {
		mailAccountSpan.innerHTML = "<font size=2 color=red>·邮箱的格式为：xxx@xxx.xxx</font>";
	});
	
	
	$("#btnCustomer").click(function(){
		var flag = true;
		checkAccount($("#account").val());
		if (!checkCustomerPwd($("#customerPwd").val() )) {
			flag = false;
		}
		if (!checkCustomerPwd($("#customerPwd").val() )) {
			flag = false;
		}
		if (!checkCustomerPwd2($("#customerPwd").val(),$("#customerPwd2").val() )) {
			flag = false;
		}
		if (!checkMailAccount($("#mailAccount").val())) {
			flag = false;
		}
		if (!checkContactInfo($("#contactInfo").val())) {
			flag = false;
		} 
		if(flag){ 
			var account = $("#account").val();
			var zz = /^\w{4,18}$/
			if (zz.test(account)){
				$.ajax({
				type : "post",
				url : "customer.do",
				data : "method=isExist&account="+account,
				cache : false,
				success:function(msg){			
					if("true"==msg){
						accountSpan.innerHTML = "<font size=2 color=red>账号已使用</font>";
					}else{
						accountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";				
						$("#customer").submit();
					}
				}
				});		
			} else {
				accountSpan.innerHTML = "<font size=2 color=red>·请输入4-18个字符,包括字母、数字、下划线!</font>";
			}
		}
	});
	
});



function checkAccount(account){
	var zz = /^\w{4,18}$/
	if (zz.test(account)){
		$.ajax({
		type : "post",
		url : "customer.do",
		data : "method=isExist&account="+account,
		cache : false,
		success:function(msg){
			
			if("true"==msg){
				accountSpan.innerHTML = "<font size=2 color=red>账号已使用</font>";
				return false;
			}else{
				accountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
				var z = /^1[3|4|5|8][0-9]\d{8}$/
				if(z.test(account)){
					var contactInfoId = document.getElementById("contactInfo");
					contactInfoId.value = account;
					contactInfoSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
				}
				return true;
			}
		}
		});		
	} else {
		accountSpan.innerHTML = "<font size=2 color=red>·请输入4-18个字符,包括字母、数字、下划线!</font>";
		return false;
	}
}

function checkCustomerPwd(customerPwd){
	var zz = /^\w{4,16}$/
	if (zz.test(customerPwd)) {
		customerPwdSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		customerPwdSpan.innerHTML = "<font size=2 color=red>·请输入4-16个字符,包括字母、数字、下划线!</font>";
		return false;
	}
}
function checkCustomerPwd2(passwords,passwords2){
	if (passwords != passwords2) {
		customerPwdSpan2.innerHTML = "<font size=2 color=red>·对不起,两次密码不一致!</font>";
		return false;
	} else {
		customerPwdSpan2.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	}
}

function checkMailAccount( mailAccount){
	
	var zz = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
	if (zz.test(mailAccount)) {
		mailAccountSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		mailAccountSpan.innerHTML = "<font size=2 color=red>·对不起,您输入的邮箱地址有误!</font>";
		return false;
	}
}

function checkContactInfo(contactInfo){
	var zz = /\d{3}-\d{8}|\d{4}-\d{7}|^1[3|4|5|8][0-9]\d{8}$/
	if (zz.test(contactInfo)) {
		contactInfoSpan.innerHTML = "<font size=2 color=blue>·OK!</font>";
		return true;
	} else {
		contactInfoSpan.innerHTML = "<font size=2 color=red>·请填写正确的固定电话或者手机号码!</font>";
		return false
	}
}



